import $service from '../../../../service.js';
var that;
const app = getApp();
Page({
  data: {
    hisType: $service.isHis,
    region: '',
    occupationList: [],
    doctorInfo: {},
    relationName: '',
    hospitalType: $service.hospitalType,
    patientInfoData:{
      "accompanyDoctorName": "", // 陪伴药师
      "healthCardNo": '',
      "id": '', 
      "patientAge": '', // 年龄
      "province": '', //省
      "city": '', // 市
      "district": "", // 区
      "patientHeight": "", // 身高
      "isDrugAllergy": "0", // 是否药物过敏
      "isMarried": "0", // 是否婚配
      "patientAddress": "", // 住址
      "patientAllergyDetails": "", // 药物过敏史
      "patientBirthday": "", // 出生日期 
      "patientIdcard": "", // 身份证号
      "patientMaritalStatus": "", // 籍贯
      "patientName": "", // 姓名
      "patientPhone": "", // 手机号码
      "patientProfession": "", // 职业名称
      "patientProfessionCode": "", // 职业Id
      "patientSex": "1", // 性别
      "patientSymptom": "", // 症状
      "patientWeight": "", // 体重
      "relationType": "", // 与您的关系
      "isMedicalHistory": 0, // 是否有既往病史
      "medicalHistory": "", // 既往史
      "isChildOption": '', // 孕期
      "isPregnancy": 0, // 是否怀孕
      "kidney": 0, // 肝功能
      "liver": 0, // 肾功能
      "paymentMethod":1  //付费方式(珠海才有其他隐藏)
    },
    relationList: [{
      text: '本人',
      value: '0'
    },{
      text: '父母',
      value: '2'
    },{
      text: '配偶',
      value: '3'
    },{
      text: '子女',
      value: '4'
    },
    // {
    //   text: '朋友',
    //   value: '5'
    // },
    {
      text: '其他',
      value: '1'
    }],
    typeList: ['备孕', '怀孕', '哺乳期'],
    isShowPart2: 0, // 是否显示part2
    isShowPart3: 0 // 是否显示part3

  },
  onLoad(e) {
    this.setData({
      'patientInfoData.id': e.id,
    })
    if (e.id) {
      this.getInquiryPatientInfoById();
    }
  },
  onShow() {
    that = this.data;
    this.getOccupationList()
  },
  //获取职业字典
  getOccupationList() {
    app.http.getOccupationList().then(res => {
      this.setData({
        occupationList: res.data.list
      })
    })
  },
  //选择职业
  occupationChange(e) {
    let val = e.detail.value;
    this.setData({
      'patientInfoData.patientProfession': this.data.occupationList[val].occupationName,
      'patientInfoData.patientProfessionCode': this.data.occupationList[val].id
    })
  },
  // picker 选择器
  pickerChange(e) {
    let {value} = e.detail
    this.setData({
      'patientInfoData.relationType': this.data.relationList[value].value,
      relationName: this.data.relationList[value].text
    })
  },
  // 地区选择器
  bindRegionChange(e) {
    let {value} = e.detail
    let region = value.join(' ')
    this.setData({
      region,
      "patientInfoData.province": value[0],
      "patientInfoData.city": value[1],
      "patientInfoData.district": value[2]
    })
  },
  //raido
  selectValue(e) {
    let this_ = this.data;
    let {type} = e.currentTarget.dataset;
    let val =  e.detail;
    console.log(type);
    switch(type){
        case "part2": 
          this_.isShowPart2 = val == 1 ? 1 : 0
          if (this_.isShowPart2  == 0) {
            this_.patientInfoData.liver = 0
            this_.patientInfoData.kidney = 0
            this_.patientInfoData.isMarried = 0
            this_.patientInfoData.isDrugAllergy = 0
            this_.patientInfoData.patientAllergyDetails = ''
            this_.patientInfoData.isPregnancy = 0
            this_.patientInfoData.isChildOption = ''
            this_.patientInfoData.isMedicalHistory = 0
            this_.patientInfoData.medicalHistory = ''
          }
          break;
        case "paymentMethod":
          this_.patientInfoData.paymentMethod = val
          break; 
        case "liver":
          this_.patientInfoData.liver = val
          break; 
        case "kidney":
          this_.patientInfoData.kidney = val
          break; 
        case "married":
          this_.patientInfoData.isMarried = val
          break;  
        case "allergy":
          this_.patientInfoData.isDrugAllergy = val
          if (val == 0) {
            this_.patientInfoData.patientAllergyDetails = ''
          }
          break; 
        case "pregnancy":
          this_.patientInfoData.isPregnancy = val == 1 ? 1 : 0
          if (val == 0) {
            this_.patientInfoData.isChildOption = ''
          }
          break;     
        case "history":
          this_.patientInfoData.isMedicalHistory = val
          if (val == 0) {
            this_.patientInfoData.medicalHistory = ''
          }
          break;   
    }
    this.setData({
      'patientInfoData.kidney' : this_.patientInfoData.kidney,
      'patientInfoData.liver' : this_.patientInfoData.liver,
      'patientInfoData.married' : this_.patientInfoData.married,
      'patientInfoData.isMarried' : this_.patientInfoData.isMarried,
      'patientInfoData.isDrugAllergy' : this_.patientInfoData.isDrugAllergy,
      'patientInfoData.isChildOption': this_.patientInfoData.isChildOption,
      'patientInfoData.isPregnancy': this_.patientInfoData.isPregnancy,
      'patientInfoData.isMedicalHistory': this_.patientInfoData.isMedicalHistory,
      'patientInfoData.medicalHistory': this_.patientInfoData.medicalHistory,
      'patientInfoData.patientAllergyDetails': this_.patientInfoData.patientAllergyDetails,
      'patientInfoData.paymentMethod': this_.patientInfoData.paymentMethod,
      isShowPart2: this_.isShowPart2,
    })
  },
  showPart3() {
    let isShowPart3 = !this.data.isShowPart3
    this.setData({
      isShowPart3
    }) 
  },
  //监听input
  inputChange(e) {
    var name = e.detail.value;
    var index = e.currentTarget.dataset.index;
    if (index == 0) {
      that.patientInfoData.patientName = name;
    } else if (index == 1) {
      that.patientInfoData.patientPhone = name;
    } else if (index == 2) {
      that.patientInfoData.patientMaritalStatus = name;
    } else if (index == 3) {
      that.patientInfoData.patientIdcard = name;
      this.getBirthdatByIdNo(name)
    } else if (index == 4) {
      that.patientInfoData.patientWeight = name;
    } else if (index == 5) {
      that.patientInfoData.patientAddress = name;
    } else if (index == 6) {
      that.patientInfoData.patientAllergyDetails = name;
    } else if (index == 7) {
      that.patientInfoData.patientHeight = name;
    } else if (index == 8) {
      that.patientInfoData.patientSymptom = name;
    } else if (index == 9) {
      that.patientInfoData.medicalHistory = name;
    } else if (index == 10) {
      that.patientInfoData.guardian = name;
    } else if (index == 11) {
      that.patientInfoData.guardianIdcard = name;
    } else if (index == 12) {
      that.patientInfoData.accompanyDoctorName = name;
    }
    this.setData({
      patientInfoData: that.patientInfoData
    })
  },
  // 通过身份证获取用户出生日期
  getBirthdatByIdNo(idNo) {
    console.log(idNo)
    if (!$service.validateElement('idCard', idNo)) {
      $service.showToast('请输入正确的身份证号');
      return;
    }
    let tmpStr = "";
    let sex = '';
    let age = ''
    if (idNo.length == 15) {
        sex = idCard.substring(14, 15) % 2
        tmpStr = idNo.substring(6, 12);
        tmpStr = "19" + tmpStr;
        age = new Date().getFullYear() - tmpStr.substring(0, 4)
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
    }else if (idNo.length == 18){
        sex = idNo.substring(14, 17) % 2
        tmpStr = idNo.substring(6, 14);
        age = new Date().getFullYear() - tmpStr.substring(0, 4)
        tmpStr = tmpStr.substring(0, 4) + "-" + tmpStr.substring(4, 6) + "-" + tmpStr.substring(6)
    }
    this.setData({
      'patientInfoData.patientBirthday': tmpStr,
      'patientInfoData.patientSex': sex,
      'patientInfoData.patientAge': age,
    })
  },
  
  //保存
  addInquiryPatientInfo() {
      if (!that.patientInfoData.patientName) {
        $service.showToast('请输入名称');
        return;
      }
      if (!that.patientInfoData.patientPhone) {
        $service.showToast('请输入手机号');
        return;
      }
      if (!$service.validateElement('mobile', that.patientInfoData.patientPhone)) {
        $service.showToast('请输入正确的手机号');
        return;
      }
      if (!that.patientInfoData.patientIdcard) {
          $service.showToast('请输入身份证号');
          return;
      }
      if (!$service.validateElement('idCard', that.patientInfoData.patientIdcard)) {
        $service.showToast('请输入正确的身份证号');
        return;
      }
      if (that.patientInfoData.relationType == '' ) {
        $service.showToast('请选择就诊人关系');
        return;
      }
      if ((that.patientInfoData.patientAge <= 6 && !$service.isEsgin) || (that.patientInfoData.patientAge <= 18 && $service.isEsgin)) {
        if (!that.patientInfoData.guardian) {
          $service.showToast('请填写监护人姓名');
          return;
        }
        if (!that.patientInfoData.guardianIdcard) {
          $service.showToast('请填写监护人身份证');
          return;
        }
        if (that.patientInfoData.accompanyDoctorName == '' ) {
          $service.showToast('请输入陪伴医师姓名');
          return;
        }
      }
      if (that.patientInfoData.isDrugAllergy === '1' ) {
        if (that.patientInfoData.patientAllergyDetails == '') {
          $service.showToast('请填写药物过敏史');
          return;
        }
      }
      if (that.patientInfoData.patientSex == '1') {
        this.setData({
          'patientInfoData.isChildOption': '',
          'patientInfoData.isMarried': '0'
        })
      }
      this.getBirthdatByIdNo(that.patientInfoData.patientIdcard)
      console.log(that.patientInfoData)
      let apiType = this.data.patientInfoData.id ? 'updatePatientInfo' : 'addPatientInfo'
      app.http[apiType](this.data.patientInfoData).then(res => {
        $service.showToast(res.msg)
        setTimeout(() => {
          wx.navigateBack({
            delta: 1,
          })
        },1000)
      }).catch(err => {
        $service.showToast(err.msg)
      })
  },
  // 获取就诊人详情
  getInquiryPatientInfoById() {
    app.http.getInquiryPatientInfoById({id: this.data.patientInfoData.id}).then(res => {
      let { patientSex,medicalHistory, district, relationType, isChildOption, liver, kidney, isDrugAllergy, patientProfession, patientAddress, patientWeight, patientHeight} = res.data
      if (liver || kidney || isDrugAllergy == '1' || medicalHistory || (isChildOption && patientSex == 0)) { // 如果有填写part2内容则打开
        this.data.isShowPart2 = 1
      }
      if (district || patientProfession || patientAddress || patientWeight || patientHeight || patientSex == 0) { // 如果有填写part3内容则打开
        this.data.isShowPart3 = 1
      }
      for (let key in res.data) {
        this.data.patientInfoData[key] = res.data[key]
      }
      if (medicalHistory) {
        this.data.patientInfoData.isMedicalHistory = 1
      }
      if (district) {
        this.data.region = res.data.province + ' ' + res.data.city + ' ' + res.data.district 
      }
      this.data.relationList.forEach((e) => {
        if (relationType == e.value) {
          this.data.relationName = e.text
        }
      })
      if (isChildOption) {
        this.data.patientInfoData.isPregnancy = 1
      }
      this.setData({
        patientInfoData: this.data.patientInfoData,
        region: this.data.region,
        relationName: this.data.relationName,
        isShowPart2: this.data.isShowPart2,
        isShowPart3: this.data.isShowPart3
      })
    }).catch(err => {
      console.log(err)
      $service.showToast(err.msg)
    })
  },
  // 删除就诊人
  del() {
    app.http.deleteInterrogationHistoryInfo({id: this.data.patientInfoData.id}).then(res => {
      $service.showToast(res.msg)
      let pages = getCurrentPages(); 
      let prevPage = pages[ pages.length - 2 ];  
      prevPage.setData({
          id: '', 
          patientName: '',
          healthCardNo: ''
        })
      setTimeout(() => {
        wx.navigateBack({
          delta: 1,
        })
      },1000)
    }).catch(err => {
      $service.showToast(err.msg)
    })
  },
  // 选择身体状况
  changeType(e) {
    let {type} = e.currentTarget.dataset
    this.data.patientInfoData.isChildOption  =  type == this.data.patientInfoData.isChildOption ? '' : type
    this.setData({
      'patientInfoData.isChildOption': this.data.patientInfoData.isChildOption 
    })
  },
})